import sun.reflect.generics.tree.Tree;

import java.util.ArrayList;
import java.util.List;

public class BinaryTree {


    static class TreeNode{
        public char val;
        public TreeNode left;
        public TreeNode right;
        public TreeNode(char val){
            this.val=val;
        }
    }

    //创建一棵二叉树，创建成功后返回根节点
    public TreeNode createTree(){
        TreeNode A=new TreeNode('A');
        TreeNode B=new TreeNode('B');
        TreeNode C=new TreeNode('C');
        TreeNode D=new TreeNode('D');
        TreeNode E=new TreeNode('E');
        TreeNode F=new TreeNode('F');
        TreeNode G=new TreeNode('G');
        TreeNode H=new TreeNode('H');

        A.left=B;
        A.right=C;
        B.left=D;
        B.right=E;
        C.left=F;
        C.right=G;
        E.left=H;

        return A;
    }

    void preOrder(TreeNode root){
        if(root==null){
            return ;
        }
        System.out.println(root.val+"");
        preOrder(root.left);
        preOrder(root.right);
    }

    void inOrder(TreeNode root){
        if(root==null){
            return ;
        }
        inOrder(root.left);
        System.out.println(root.val+"");
        inOrder(root.right);
    }
    void postOrder(TreeNode root){
        if(root==null){
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.println(root.val+"");
    }

    //前序遍历后存到链表中
    public List<Integer> preorderTraversal(TreeNode root){
        List<Integer> list=new ArrayList<>();
        if(root==null){
            return list;
        }
        list.add(root.val);
        List<Integer> leftTree=preorderTraversal(root.left);
        list.addAll(leftTree);
        List<Integer> rightTree=preorderTraversal(root.left);
        list.addAll(rightTree);
        return list;
    }
}
